package com.r_z.service.impl;

import com.r_z.mapper.FiscalExpenditureMapper;
import com.r_z.service.FiscalExpenditureService;
import org.springframework.stereotype.Service;
import com.r_z.entity.FiscalExpenditure;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 财务支出
 * @author :yimeng
 * @date :2023/5/30  8:53
 * @description:some description
 */
@Service
public class FiscalExpenditureServiceImpl implements FiscalExpenditureService {
    @Resource
    FiscalExpenditureMapper fiscalExpenditureMapper;

    /**
     * 根据主键ID查询财务信息
     * @param fExpenditureId
     * @return
     */
    @Override
    public List<FiscalExpenditure> selectAllFiscalExpenditure(String fExpenditureId) {
        return fiscalExpenditureMapper.selectAllFiscalExpenditure(fExpenditureId);
    }

    /**
     * 根据条件分页查询
     * @param page 当前页
     * @param limit 每页显示数
     * @param fiscalExpenditure
     * @return
     */
    @Override
    public List<FiscalExpenditure> LimitFiscalExpenditure(int page,int limit,FiscalExpenditure fiscalExpenditure) {
        Map map = new HashMap();
        map.put("page",(page-1)*limit);
        map.put("limit",limit);
        map.put("strWhere",buildStrWhere(fiscalExpenditure));
        return fiscalExpenditureMapper.LimitFiscalExpenditure(map);
    }

    /**
     * 根据条件查询总行数
     * @param fiscalExpenditure
     * @return
     */
    @Override
    public int countFiscalExpenditure(FiscalExpenditure fiscalExpenditure) {
        Map map = new HashMap();
        map.put("strWhere",buildStrWhere(fiscalExpenditure));
        int count = fiscalExpenditureMapper.countFiscalExpenditure(map);
        return count;
    }

    /**
     * 添加财务支出信息
     * @param fiscalExpenditure
     * @return
     */
    @Override
    public int insertFiscalExpenditure(FiscalExpenditure fiscalExpenditure) {
        return fiscalExpenditureMapper.insertFiscalExpenditure(fiscalExpenditure);
    }

    /**
     * 根据主键ID进行删除财务支出信息
     * @param fExpenditureId
     * @return
     */
    @Override
    public int deleteFiscalExpenditure(String fExpenditureId) {
        return fiscalExpenditureMapper.deleteFiscalExpenditure(fExpenditureId);
    }

    /**
     * 批量删除
     * @param fExpenditureId
     * @return
     */
    @Override
    public int deleteFiscalExpenditureById(String fExpenditureId) {
        //转数据
        String[] split = fExpenditureId.split(",");
        //创建存放id的集合
        ArrayList<Object> objects = new ArrayList<>();
        for (String id : split){
            //将id存入集合
            objects.add(id);
        }
        return fiscalExpenditureMapper.deleteFiscalExpenditureById(objects);
    }

    /**
     * 根据主键ID进行修改财务支出信息
     * @param fiscalExpenditure
     * @return
     */
    @Override
    public int updateFiscalExpenditure(FiscalExpenditure fiscalExpenditure) {
        return fiscalExpenditureMapper.updateFiscalExpenditure(fiscalExpenditure);
    }


    /**
     * 生成条件字符串
     * @param fiscalExpenditure 生成条件的参数
     * @return 条件字符串  and fundId like '%%'
     */
    public String buildStrWhere(FiscalExpenditure fiscalExpenditure){
        StringBuffer strWhere = new StringBuffer("");
        if(fiscalExpenditure.getFExpenditureId()!=null && !fiscalExpenditure.getFExpenditureId().equals(""))
        {
            strWhere.append(" and fExpenditureId like '%"+fiscalExpenditure.getFExpenditureId ()+"%'");
        }
        return  strWhere.toString();
    }
}
